<?php

class AjaxController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function PreDispatch()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    }

    public function indexAction()
    {
        
           $markersType = $this->_getParam('markersType');

           
           $json = '{"markers":['."\n";
           //получаем список маркеров из бд
           $object = new Application_Model_Markers();
           $mark = $object->selectMarkers($markersType);
           
           //получаем иконки выбранной категории
           $icons = $object->getCategoryIcon($markersType);
           $markerImg = $icons[0]['icon'];
           //var_dump($markerImg);
           if($mark) {
               foreach ($mark as $key => $par){
  
               $json.= "\n".'{'.'"mname": "'.$par['name'].'",';
 
               $json.= '"address": "'.$par['address'].'",';

               $json.= '"type": "'.$par['type'].'",';
 
               $json.= '"lat": "'.$par['lat'].'",';
 
               $json.=  '"lon": "'.$par['lng'].'",';
               
             //  $json.= '"icon": "'.$icon['icon'].'",';
               
               $json.= '"mimg": "'.$markerImg.'"';
 
               $json.= '},';

               }


               $json = substr($json, 0,-1);
                
               $this->_response->appendBody($json);
 
               echo '], ', "\n", '"mimg": "',$markerImg.'",',"\n" ,'"status": "OK"', "\n", '}';
           
          }
    
    }

    public function getpointsAction()
    {
        $object = new Application_Model_Gps();
        //получаем imei
        $imei = $this->_getParam('imei');
        $id_imei = $object->getImeiId($imei);             
        $points = $object->getAllPoints($id_imei['id']);
        
        $json = '{"points":['."\n";
        
        if($points){
            //формируем данные в формате jason
            foreach($points as $key => $point){
               
               $json.= "\n".'{'.'"imei": "'.$point['id_imei'].'",';
 
               $json.= '"longitude": "'.$point['longitude'].'",';

               $json.= '"latitude": "'.$point['latitude'].'",';
 
               $json.= '"speed": "'.$point['speed'].'",';
 
               $json.=  '"battery": "'.$point['battery'].'",';
               
               $json.= '"date": "'.$point['date'].'"';
          
               $json.= '},';
            }
            
            $json = substr($json, 0,-1);
                
               $this->_response->appendBody($json);
 
               echo '], ', "\n", "\n" ,'"status": "OK"', "\n", '}';
        }
    }

    public function getlastpointAction()
    {
        $markerImg = '1';
        $object = new Application_Model_Gps();
        //получаем imei
        $imei = $this->_getParam('imei');
        $id_imei = $object->getImeiId($imei);        
        $points = $object->getLastPoint($id_imei['id']);
        
        $json = '{"points":['."\n";
        
        if($points){
            //формируем данные в формате json
            foreach($points as $key => $point){
               
               $json.= "\n".'{'.'"imei": "'.$point['id_imei'].'",';
 
               $json.= '"longitude": "'.$point['longitude'].'",';

               $json.= '"latitude": "'.$point['latitude'].'",';
 
               $json.= '"speed": "'.$point['speed'].'",';
 
               $json.=  '"battery": "'.$point['battery'].'",';
               
               $json.= '"date": "'.$point['date'].'"';
          
               $json.= '},';
            }
            
            $json = substr($json, 0,-1);   
            $this->_response->appendBody($json);
            echo '], ', "\n", "\n" ,'"status": "OK"', "\n", '}';
        }  
    }

    public function newcarAction()
    {
        $obj = new Application_Model_Gps();                
        $id_user = Zend_Auth::getInstance()->getIdentity()->id;          
        if ($this->getRequest()->isPost()) {                     
            $imei = $this->getRequest()->getPost('imei');
            $name = $this->getRequest()->getPost('name');
            $id_category = $this->getRequest()->getPost('category');
                if ($imei != null && $name != null) {
                    $data = array(
                        'id_user'     => $id_user,
                        'name'        => $name,
                        'id_category' => $id_category,
                        'imei'        => $imei                    
                    );

                    $id_imei = $obj->addImei($data);
                    $data2 = array(
                        'id_imei' => $id_imei,
                        'id_user' => $id_user,
                        'name'    => $name.' '.date('d-m-Y')                    
                    );
                    $obj->newRoute($data2);
                }     
        }
    }

    public function getallcarsAction()
    {
        $object = new Application_Model_Gps();
        //получаем imei
        $id_user = Zend_Auth::getInstance()->getIdentity()->id;
        $cars = $object->getAllImei($id_user);                             
        $json = '{"cars":['."\n";        
        if($cars) {
            //формируем данные в формате jason
            foreach ($cars as $key => $car){
               
               $json.= "\n".'{'.'"imei": "'.$car['imei'].'",'; 
               $json.= '"name": "'.$car['name'].'"';
               $json.= '},';
            }
            
            $json = substr($json, 0,-1);
                
               $this->_response->appendBody($json);
 
               echo '], ', "\n", "\n" ,'"status": "OK"', "\n", '}';
        } 
    
    }

    public function getfullpointsAction()
    {
        $object = new Application_Model_Gps();
        //получаем imei
        $id_route = $this->_getParam('id');
        //$id_imei = $object->getImeiId($imei);             
        $points = $object->getAllPoints($id_route);
        
        $json = '{"points":['."\n";
        
        if($points){
            //формируем данные в формате jason
            foreach($points as $key => $point){
               
               $json.= "\n".'{'.'"imei": "'.$point['id_imei'].'",';
 
               $json.= '"longitude": "'.$point['longitude'].'",';

               $json.= '"latitude": "'.$point['latitude'].'",';
 
               $json.= '"speed": "'.$point['speed'].'",';
 
               $json.=  '"battery": "'.$point['battery'].'",';
               
               $json.= '"date": "'.$point['date'].'"';
          
               $json.= '},';
            }
            
            $json = substr($json, 0,-1);
                
               $this->_response->appendBody($json);
 
               echo '], ', "\n", "\n" ,'"status": "OK"', "\n", '}';
        }
    }

    public function getonepointAction()
    {
        $object = new Application_Model_Gps();
        //получаем imei
        $id_imei = $this->_getParam('id');
        //$id_imei = $object->getImeiId($imei);        
        $points = $object->getTwoLastPoints($id_imei);
        
        $json = '{"points":['."\n";
        
        if($points){
            //формируем данные в формате json
            foreach($points as $key => $point){
               
               $json.= "\n".'{'.'"imei": "'.$point['id_imei'].'",';
 
               $json.= '"longitude": "'.$point['longitude'].'",';

               $json.= '"latitude": "'.$point['latitude'].'",';
 
               $json.= '"speed": "'.$point['speed'].'",';
 
               $json.=  '"battery": "'.$point['battery'].'",';
               
               $json.= '"date": "'.$point['date'].'"';
          
               $json.= '},';
            }
            
            $json = substr($json, 0,-1);   
            $this->_response->appendBody($json);
            echo '], ', "\n", "\n" ,'"status": "OK"', "\n", '}';
        }  
    }


}








